package javaParser;
/**
 * @author Sindhu
 */

import java.io.*;
import com.hp.hpl.jena.rdf.model.*;
import java.util.ArrayList;

public class TreeToRdf {

	//scan RdfTree and create rdf model
	public Model ToRdf(RdfTree tree, SourceOntology ontology){
//
		int numClasses = tree.size();
		String outputFileName = Main.getXMLName();
		Resource[] classList = new Resource[numClasses];
		ArrayList<Resource> methodList = new ArrayList<Resource>();


		// Create an empty graph
		Model model = ModelFactory.createDefaultModel();

		// Generate all the Classes,Methods and name each one.
	
		for(int i = 0 ; i < numClasses ; i++)
		{
			String Classname = tree.get(i).getName();
			
			classList[i] = model.createResource(ontology.oClass());
			classList[i].addProperty(ontology.oNamed(), Classname );  
			
			for(int j=0 ; j <tree.get(i).sizeOfFields() ; j++)
			{
				String FieldName = tree.get(i).getField(j).getName();
				String FieldType = tree.get(i).getField(j).getType();
				String FieldMod  = tree.get(i).getField(j).getModifier();
				
				Resource FieldProperty = model.createResource(ontology.oField());
				FieldProperty.addProperty(ontology.oType(), FieldType);
				FieldProperty.addProperty(ontology.oNamed(), FieldName);
				if (FieldMod != null)
				{
					FieldProperty.addProperty(ontology.oModifier(), FieldMod);
				}
				classList[i].addProperty(ontology.oHasField(), FieldProperty);
			}
			
			for(int j=0 ; j <tree.get(i).sizeOfMethods() ; j++)
			{
				String Methodname = tree.get(i).getMethod(j).getName();
				String MethodReturn = tree.get(i).getMethod(j).getType();
				
				Resource methodProperty = model.createResource(ontology.oMethod());
				
				for(int k=0; k <tree.get(i).getMethod(j).sizeOfParams() ; k++)
				{
					String ParameterName = tree.get(i).getMethod(j).getParam(k).getName();
					String ParameterType = tree.get(i).getMethod(j).getParam(k).getType();
					
					Resource parameterProperty = model.createResource(ontology.oParameter());
					parameterProperty.addProperty(ontology.oType(), ParameterType);
					parameterProperty.addProperty(ontology.oNamed(), ParameterName);
					methodProperty.addProperty(ontology.oHasParameter(),parameterProperty);
				}
				
				methodProperty.addProperty(ontology.oNamed(), Methodname);
				methodProperty.addProperty(ontology.oType(), MethodReturn);
				methodList.add(methodProperty);
				classList[i].addProperty(ontology.oHasMethod(), 
						methodProperty);
			}
		}
		//Sindhu:  generate the "Calls" relationships for each method.
		
		for(int i = 0 ; i < numClasses ; i++)
		{
			for(int j=0 ; j <tree.get(i).sizeOfMethods() ; j++)
			{			
				for(int k=0 ; k<tree.get(i).getMethod(j).sizeOfCalls() ; k++ )
				{
					RdfMethod tempMethod = tree.getMethod(tree.get(i),tree.get(i).getMethod(j).getCalls(k));
					if (tempMethod!=null) {
						Resource calling = null;
						Resource called = null;
						String Calls = tempMethod.getName();
						//find method calling
						for (int m=0; m<methodList.size(); m++) {
							if (tree.get(i).getMethod(j).getName().equals(methodList.get(m).getProperty(ontology.oNamed()).getString())) {
								calling = methodList.get(m);
							}
						}
						//find method called
						for (int m=0; m<methodList.size(); m++) {
							if (Calls.equals(methodList.get(m).getProperty(ontology.oNamed()).getString())) {
								called = methodList.get(m);
							}
						}
						
						if (calling != null && called != null) {
							calling.addProperty(ontology.oCalls(), called);
						}
					}
				}
			}
		}
		
		// Create a file to output the RDF/XML and return the model.
		FileOutputStream rdfFile = null;
		
		try{
			rdfFile = new FileOutputStream(outputFileName);
		}
		catch(FileNotFoundException e)
		{
			System.err.println("Could not open file: " + outputFileName + ". Reason: " + e.getMessage() );

		}

		model.write(rdfFile, "RDF/XML");
		return model;
	}

}
